package com.lineage.server.timecontroller.pc;

import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.thread.PcOtherThreadPool;
import com.lineage.server.world.World;
import com.lineage.server.world.WorldItem;
import com.lineage.william.ItemOutburst;
import java.util.Collection;
import java.util.Iterator;
import java.util.Random;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Quburcount extends TimerTask {
  private static final Log _log = LogFactory.getLog(Quburcount.class);
  
  private static final Random _random = new Random();
  
  private ScheduledFuture<?> _timer;
  
  public void start() {
    int timeMillis = 1000;
    this._timer = PcOtherThreadPool.get().scheduleAtFixedRate(this, 1000L, 1000L);
  }
  
  public void run() {
    try {
      Collection<L1ItemInstance> items = WorldItem.get().all();
      Collection<L1PcInstance> all = World.get().getAllPlayers();
      if (all.isEmpty())
        return; 
      Iterator<L1PcInstance> iter = all.iterator();
      while (iter.hasNext()) {
        L1PcInstance tgpc = iter.next();
        if (check(tgpc) && tgpc.getQuburcount() > 0) {
          tgpc.setQuburcount(tgpc.getQuburcount() - 1);
          switch (tgpc.getQuburcount()) {
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
            case 8:
            case 9:
            case 10:
            case 11:
            case 12:
            case 13:
            case 14:
            case 15:
            case 16:
            case 17:
            case 18:
            case 19:
            case 20:
              tgpc.sendPackets((ServerBasePacket)new S_ServerMessage("目前爆气值剩余:" + tgpc.getQuburcount()));
              break;
          } 
          if (tgpc.getQuburcount() != 0)
            continue; 
          Iterator<L1ItemInstance> iter2 = items.iterator();
          while (iter2.hasNext()) {
            L1ItemInstance item = iter2.next();
            ItemOutburst.falseOutburst(tgpc, item);
          } 
        } 
      } 
    } catch (Exception e) {
      _log.error("爆气时间轴异常重启", e);
      PcOtherThreadPool.get().cancel(this._timer, false);
      Quburcount partyTimer = new Quburcount();
      partyTimer.start();
    } 
  }
  
  private static boolean check(L1PcInstance tgpc) {
    try {
      if (tgpc == null)
        return false; 
      if (tgpc.getOnlineStatus() == 0)
        return false; 
      if (tgpc.getNetConnection() == null)
        return false; 
      if (!tgpc.getcheckOutbur())
        return false; 
      if (!tgpc.getOutbur())
        return false; 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
      return false;
    } 
    return true;
  }
}
